<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { Lock } from '@element-plus/icons-vue';
import { useAppUserStore } from '@/stores';
import { loading } from '@/utilities/loading';
import { appUserChangePasswordService } from '@/apis/appuser';

const appUserStore = useAppUserStore();
const router = useRouter();

const formRef = ref();
const formModel = ref({
  originPassword: '',
  newPassword: '',
  rePassword: ''
});

const rules = ref({
  originPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15 位的非空字符',
      trigger: 'blur'
    }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15 位的非空字符',
      trigger: 'blur'
    }
  ],
  rePassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15 位的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.newPassword) {
          callback(new Error('两次输入密码不一致'));
        } else {
          callback(); // 校验成功后也需要 callback
        }
      },
      trigger: 'blur'
    }
  ]
});

const submitForm = async () => {
  // 等待校验结果
  await formRef.value.validate();
  // 提交修改
  let res = await appUserChangePasswordService(formModel.value);
  // 提示用户
  ElMessage.success(res.msg);

  appUserStore.removeTokenObj();
  appUserStore.setUserInfo({});
  router.push('/login');
};
</script>

<template>
  <page-container title="修改密码">
    <el-form
      v-loading="loading"
      ref="formRef"
      :model="formModel"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="originPassword">
        <el-input
          v-model="formModel.originPassword"
          :prefix-icon="Lock"
          type="password"
          show-password
          placeholder="请输入原密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model="formModel.newPassword"
          :prefix-icon="Lock"
          type="password"
          show-password
          placeholder="请输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="再次输入" prop="rePassword">
        <el-input
          v-model="formModel.rePassword"
          :prefix-icon="Lock"
          type="password"
          show-password
          placeholder="请再次输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="submitForm"
          class="button"
          type="primary"
          auto-insert-space
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </page-container>
</template>

<style lang="scss" scoped>
.el-form .el-form-item {
  width: 500px;
}
</style>
